package com.gskorupa.univermon.heartbeat;

import com.google.gson.Gson;
import java.io.IOException;
import java.util.Date;
import javax.servlet.http.*;
//import com.google.appengine.api.users.User;
//import com.google.appengine.api.users.UserService;
//import com.google.appengine.api.users.UserServiceFactory;

public class HeartBeat extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        //
        resp.setContentType("application/json");
        Gson gson = new Gson();
        String addr = req.getRemoteAddr();
        String query = req.getParameter("query");
        Entity ent = new Entity(addr, new Date());
        
        if (query == null) {
            //store entity
            // ...
            
            resp.getWriter().println(gson.toJson("Hello "+addr));
        }else if(query.equalsIgnoreCase("error")){
            resp.sendError(501, "forced");
    }else {    
            Report report = new Report();
            report.setClient(addr);
            report.setQuery(query);

            //request DB
            report.append(ent);
            report.append(ent);

            //send response
            resp.getWriter().println(gson.toJson(report));
        }
    }
}
